package com.bishe.utils;

import lombok.Data;

import java.util.List;

/**
 * 自定义返回结果
 * @param <T>
 */
@Data
public class PageResult<T> {
    private Integer total; //数据连个

    private Integer pageSize; //一页数据

    private Integer pageNo;//页数

    private List<T> result;// 数据

    public PageResult(OrderPage orderPage, Integer total, List<T> result){
        this.pageNo = orderPage.getPageNo();
        this.pageSize = orderPage.getPageSize();
        this.total = total;
        this.result = result;
    }

    //数据封装方法
    public PageResult(OrderPage orderPage, SqlQueryUtils<T> sqlQueryUtils, List<T> result){
        //获取sql
        String sql = sqlQueryUtils.getSql();
        //根据分页切割
        String[] s = sql.split("limit");
        //存在分页  去掉参数
        if (s.length >1){
            List<Object> param = sqlQueryUtils.getParam();
            param.remove(param.size()-1);
            param.remove(param.size()-1);
        }
        //设置sql
        sqlQueryUtils.setSql(s[0]);
        //获取总数
        this.total = sqlQueryUtils.getCount();
        this.pageNo = orderPage.getPageNo();
        this.pageSize = orderPage.getPageSize();
        this.result = result;
    }
}
